package com.laojiang.imagepickers.widget;

import android.content.res.Resources;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * <pre>
 *     author : wxq
 *     time   : 2018/12/11
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public class MyItemDecoration extends RecyclerView.ItemDecoration {

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
//            int spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            int spanSize = ((GridLayoutManager) layoutManager).getSpanSizeLookup().getSpanSize(parent.getChildAdapterPosition(view));
            if (spanSize == 3) {
                outRect.left = 0;
                outRect.top = dp2px(1);
            } else {
                outRect.left = dp2px(3);
                outRect.top = dp2px(3);
            }
        }
    }

    private int dp2px(int dp) {
        Resources system = Resources.getSystem();
        int density = (int) system.getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }
}
